<?php

include "../controller/ConectBanco.php";
header("Content-Type: application/json");

$jsonData ='';
//$params="1@2013-09-25 23:40:08@@2@2013-10-26 18:22:55";
$params = $_POST["params"];
$jsonData .='{"ponto":[';

$arrayParams = explode("@@",$params);
foreach ($arrayParams as $indice => $valor) {
	$arrayDateTime = explode("@",  $valor);
	$idDevice = $arrayDateTime[0];
	$dateTime = date("Y-m-d H:i:s",strtotime($arrayDateTime[1]));
	$devices  = mysql_query("select * from ponto where idDevice = $idDevice" ) or die (mysql_error());
	
	$pt = mysql_fetch_array($devices);
	$jsonData .= '{"id":"'.$pt["idDevice"].'"
			      ,"nome":"'.$pt["nomePonto"].'"
				  ,"info":"'.$pt["info"].'"
				  ,"icon":"'.$pt["icone"].'"
				  ,"posicoes":[';
		
	$posicoes = mysql_query("select distinct latitude, longitude,velocidade,datetime,last
							from package
							where iddevice = $idDevice
							and typeData = 12
							and latitude <> '-0.000000'
							and longitude <> '-0.000000'
							and datetime > '".$dateTime."' ")or die (mysql_error());

	while($row = mysql_fetch_array($posicoes)){

		$jsonData .= '{"latitude"   :"'.$row["latitude"].'",
				       "longitude"  :"'.$row["longitude"].'",
				       "last"       :"'.$row["last"].'",
					   "velocidade" :"'.$row["velocidade"].'",
					   "dateTime"   :"'.date("H:i:s d-m-Y",strtotime($row["datetime"])).'"},';

	}
	$jsonData = chop($jsonData,",");
	$jsonData .=']},';
}
$jsonData = chop($jsonData,",");
$jsonData .= ']}';
echo $jsonData;
?>